package com.bigflexlib.x.mx.core.Container
{
	import mx.collections.ArrayCollection;
	import mx.core.Container;
	import mx.events.ChildExistenceChangedEvent;

	/**
	 * 
	 * Wraps a Container and exposes its children as an ArrayCollection.
	 * TODO: We still need to implement a strategy to detect z-ordering ( there are no events dispatched ).
	 * Perhaps using a timer is the only viable strategy.
	 * 
	 * @author aldo.bucchi@gmail.com
	 * 
	 */
	public class ContainerChildrenList extends ArrayCollection
	{

		private var container:Container;
		
		public function ContainerChildrenList( container:Container )
		{
			this.container = container;
		
			for ( var i:int=0; i<container.numChildren; i++ )
				addItem( container.getChildAt( i ) );			

			container.addEventListener( ChildExistenceChangedEvent.CHILD_ADD, onChildAdd );
			container.addEventListener( ChildExistenceChangedEvent.CHILD_REMOVE, onChildRemove );
		}


		private function onChildAdd( event:ChildExistenceChangedEvent ):void
		{
			addItem( event.relatedObject );
		}


		private function onChildRemove( event:ChildExistenceChangedEvent ):void
		{
			removeItemAt( getItemIndex( event.relatedObject ) );
		}

	}

}